<?php

/**
 * sfRichTextEditorWmd implements the Wmd rich text markdown editor.
 */
class sfRichTextEditorWmd extends sfRichTextEditor
{
  /**
   * Returns the rich text editor as HTML.
   *
   * @return string Rich text editor HTML representation
   */
  public function toHTML()
  {
    $options = $this->options;

    // we need to know the id for things the rich text editor
    // in advance of building the tag
    $id = _get_option($options, 'id', $this->name);

    $optionFile = _get_option($options, 'wmd_options_file');
    if (!$optionFile || !is_readable(sfConfig::get('sf_web_dir').'/js/'.$optionFile))
      $optionFile = is_readable(sfConfig::get('sf_web_dir').'/js/wmd-options.js') ? 'wmd-options' : '/sfWmdPlugin/js/wmd-options';     
    
    sfContext::getInstance()->getResponse()->addJavascript($optionFile);
    sfContext::getInstance()->getResponse()->addJavascript('/sfWmdPlugin/js/showdown');
    sfContext::getInstance()->getResponse()->addJavascript('/sfWmdPlugin/js/wmd-base');
    sfContext::getInstance()->getResponse()->addJavascript('/sfWmdPlugin/js/wmd-plus');
    sfContext::getInstance()->getResponse()->addJavascript('/sfWmdPlugin/js/wmd');

    $wmd_options = isset($options['wmd_options']) ? $options['wmd_options'] : false;
    
    if (isset($options['wmd_options'])) {
        unset($options['wmd_options']);
    }
    
    $rich_text_wmd_image_browser_url = sfConfig::get('sf_rich_text_wmd_image_browser_url');
    if ($wmd_options && isset($wmd_options['wmd_image_browser_url']))
        $rich_text_wmd_image_browser_url = $wmd_options['wmd_image_browser_url'];
        
    $noPreview = sfConfig::get('sf_rich_text_no_preview');
    if ($wmd_options && isset($wmd_options['wmd_no_preview']))
        $noPreview = $wmd_options['wmd_no_preview'];
    
    if (!isset($options['cols']))
        $options['cols'] = 80;
    if (!isset($options['rows']))
        $options['rows'] = 20;
     
  
    $result = content_tag('textarea', $this->content, array_merge(array('name' => $this->name, 'id' => get_id_from_name($id, null)), _convert_options($options)));
    
    if (!(true === $noPreview)) {
      $result .= '<div class="wmd-preview-labels">';
      $result .=  select_tag('wmd_preview_select_'.$this->name, options_for_select(array(0 => 'Preview', 1 => 'Syntax Guide', 2 => 'Hide')), array('id' => 'wmd_preview_select_'.get_id_from_name($id, null), 'style' => 'padding:3px;', 'onchange' => "if(document.getElementById('wmd_preview_select_".get_id_from_name($id, null)."').selectedIndex==2){document.getElementById('wmd_preview_".get_id_from_name($id, null)."').style.display='none';document.getElementById('wmd_syntax_".get_id_from_name($id, null)."').style.display='none';}else if(document.getElementById('wmd_preview_select_".get_id_from_name($id, null)."').selectedIndex==1){document.getElementById('wmd_preview_".get_id_from_name($id, null)."').style.display='none';document.getElementById('wmd_syntax_".get_id_from_name($id, null)."').style.display='block';}else if(document.getElementById('wmd_preview_select_".get_id_from_name($id, null)."').selectedIndex==0){document.getElementById('wmd_preview_".get_id_from_name($id, null)."').style.display='block';document.getElementById('wmd_syntax_".get_id_from_name($id, null)."').style.display='none'};"));
      $result .= '</div>';
    
      $result .= '<div id="wmd_preview_'.get_id_from_name($id, null).'" class="wmd-preview"></div>';
          
      $result .= '<div id="wmd_syntax_'.get_id_from_name($id, null).'" class="wmd-syntax" style="display:none;">
  <textarea readonly="readonly" cols="80" rows="20" class="syntax-pane" id="syntax_pane_'.get_id_from_name($id, null).' style="display:block;overflow:auto;">';
      
      $syntax_path = sfConfig::get('sf_rich_text_wmd_syntax_file');
      if (!is_readable($syntax_path))
      {
          $syntax_path = dirname(__file__).'/syntax.php';
      }
          
      $result .= file_get_contents($syntax_path);
      $result .= '</textarea></div>';
    }
    
    if (!function_exists('javascript_tag'))
        try {
            use_helper('Javascript');
        } catch (Exception $e) { }; 
    
    if (!function_exists('javascript_tag'))
        use_helper('JavascriptBase');
    
    
    $script = "
/***** build the preview manager *****/
var panes_".get_id_from_name($id, null)." = {input:document.getElementById('".get_id_from_name($id, null)."'), preview:document.getElementById('wmd_preview_".get_id_from_name($id, null)."'), output:null};
var previewManager = new Attacklab.wmd.previewManager(panes_".get_id_from_name($id, null).");

/***** build the editor and tell it to refresh the preview after commands *****/\n";
    $script .= "var editor_".get_id_from_name($id, null)." = new Attacklab.wmd.editor(document.getElementById('".get_id_from_name($id, null)."'),previewManager.refresh);";
    if ($rich_text_wmd_image_browser_url)
        $script .= "\nwmd_options['imageBrowserUrl']='".url_for($rich_text_wmd_image_browser_url)."';\n";
    
    $result .= javascript_tag($script);
    
    return $result;
  }
}
